package com.bdb.myswipemultitype.swipeListview.swipeMenuRecycle;

import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bdb.myswipemultitype.R;

import me.drakeet.multitype.ItemViewProvider;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

/**
 * Created by zinc on 2017/5/15.
 */

public abstract class SwipeProvider<T> extends ItemViewProvider<T, SwipeProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.swipe_item_layout, parent, false);
        Log.i("vh", "onCreateViewHolder: new");
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull T t) {

        holder.rightMenu.setText(getMenuName());
        holder.rightMenu.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), getMenuColor()));
        holder.rightMenu.setBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), getMenuBgColor()));

        LayoutInflater inflater = (LayoutInflater) holder.itemView.getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        FrameLayout frameLayout = holder.fragmentContainer;

        View view = null;

        if (frameLayout.getChildCount() <= 0) {
            view = inflater.inflate(getContentLayoutId(), frameLayout, false);
            frameLayout.addView(view, view.getLayoutParams());
        } else {
            view = frameLayout.getChildAt(0);
        }

        onBindSwipeViewHolder(holder, view, t);

    }

    abstract protected int getContentLayoutId();
    abstract protected void onBindSwipeViewHolder(@NonNull ViewHolder holder, @NonNull View view, @NonNull T t);

    //设置menu名称
    protected String getMenuName(){
        return "删除";
    }

    //设置menu背景颜色
    protected int getMenuBgColor(){
        return R.color.swipe_color;
    }

    //设置menu字体颜色
    protected int getMenuColor(){
        return R.color.cl_white;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        SwipeItemLayout swipe;
        TextView rightMenu;
        FrameLayout fragmentContainer;

        ViewHolder(View itemView) {
            super(itemView);
            swipe = (SwipeItemLayout) itemView.findViewById(R.id.swipe);
            rightMenu = (TextView) itemView.findViewById(R.id.right_menu);
            fragmentContainer = (FrameLayout) itemView.findViewById(R.id.fragment_container);
        }
    }

}
